//
//  CollectTableViewCell.m
//  Ting
//
//  Created by 安静的美男子 on 14/11/12.
//  Copyright (c) 2014年 安静的美男子. All rights reserved.
//

#import "CollectTableViewCell.h"
#import "MainCollectionViewCell.h"
@implementation CollectTableViewCell

- (void)awakeFromNib {
    // Initialization code
}
-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
    }
    return self;
}
-(void)setCollectionArr:(NSMutableArray *)arr flag:(BOOL)flag
{
    
    _flag = flag;
    self.DataArr = [NSMutableArray arrayWithArray:arr];
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.itemSize = CGSizeMake(75, 70);
    layout.minimumInteritemSpacing = 0;
    if (!_MyCollection) {
        self.MyCollection = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, ( arr.count / 4 + 1 )* 80) collectionViewLayout:layout];
        
    }
    if (flag == NO) {
       self.MyCollection.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width,  2* 80);
    }
    else{
    self.MyCollection.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, ( arr.count / 4 + 1 )* 80);
    }
    self.MyCollection.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:_MyCollection];
    _MyCollection.delegate = self;
    _MyCollection.dataSource = self;
    _MyCollection.scrollEnabled = NO;
    [_MyCollection registerClass:[MainCollectionViewCell class ]forCellWithReuseIdentifier:@"cell2"];
    //每次重用reloaddata
    [_MyCollection reloadData];
    
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (_flag) {
        
        return _DataArr.count + 1;
    }
    else
    {
        if (_DataArr.count == 0) {
            return 0;
        }
        else
        {
      return  8;
        }
    }
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{

    MainCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell2" forIndexPath:indexPath];
    
    [cell setArray:_DataArr indexpath:indexPath.item flag:_flag];
    
    return cell;
    
    
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (_flag == NO) {
        if (indexPath.item == 7) {
            self.MyCollection.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 5* 80);
            [self.delegate changViewWithFlag:!_flag];
        }else
        {
    [self.delegate pushViewWithnum:indexPath.item];
        }
    }else if (_flag == YES)
    {
        if (indexPath.item == 19) {
            self.MyCollection.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 2* 80);
            [self.delegate changViewWithFlag:!_flag];
        }else
        {
            [self.delegate pushViewWithnum:indexPath.item];

        }
    }
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
